package util

import java.util.Date
import java.util.Calendar
import java.text.SimpleDateFormat
import java.text.DateFormat

case class EPDate(millis: Long) {
  def date() = { val d = new Date(); d.setTime(millis); d }
  def calendar() = { val c = Calendar.getInstance(); c.setTimeInMillis(millis); c }

  def plus(ammount: Int): DateTransformer = {
    new DateTransformer(this, ammount, 1)
  }

  def minus(ammount: Int): DateTransformer = {
    new DateTransformer(this, ammount, -1)
  }
}

object EPDate {
  val df = new SimpleDateFormat("yyyy/MM/dd")
  def apply(string: String): EPDate = {
    apply(string, df)
  }

  def apply(string: String, df: DateFormat): EPDate = {
    val date = df.parse(string)
    EPDate(date.getTime())
  }
}

case class DateTransformer(epDate: EPDate, v: Int, operation: Int) {
  def years = {
    operate(Calendar.YEAR)
  }

  def months = {
    operate(Calendar.MONTH)
  }

  def days = {
    operate(Calendar.DATE)
  }

  private def operate(field: Int) = {
    var c = Calendar.getInstance()
    c.setTimeInMillis(epDate.millis)
    c.add(field, v * operation)

    new EPDate(c.getTimeInMillis())
  }
}
